package org.springblade.resource.feign;

import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.tool.api.R;
import org.springblade.resource.builder.oss.OssBuilder;
import org.springblade.resource.constant.OssConstant;
import org.springblade.resource.vo.SafeBladeFile;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@AllArgsConstructor
public class OssClient implements IOssClient {

    private final OssBuilder ossBuilder;

    @SneakyThrows
    @Override
    public R<SafeBladeFile> putFile(@RequestPart("file") MultipartFile file) {
        BladeFile bladeFile = ossBuilder.template().putFile(file.getOriginalFilename(), file.getInputStream());
        SafeBladeFile safeBladeFile = OssConstant.toSafeBladeFile(bladeFile);
        return R.data(safeBladeFile);
    }

    @SneakyThrows
    @Override
    public R<SafeBladeFile> putFile(@RequestParam String fileName, @RequestPart("file") MultipartFile file) {
        BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
        SafeBladeFile safeBladeFile = OssConstant.toSafeBladeFile(bladeFile);
        return R.data(safeBladeFile);
    }

}
